﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.Controls.Tools;
using GQ.BLL.QuanLyKho;
using GQ.Utils;
using GQ.WinForms;

namespace GQ.QuanLyKho
{
    public partial class frmListHangHoa : frmBase
    {
        public frmListHangHoa()
        {
            InitializeComponent();
            GridUtils.SetStyle(gridView1);
            gridView1.OptionsBehavior.AutoExpandAllGroups = true;
        }

        private void frmListHangHoa_Load(object sender, EventArgs e)
        {
            Singleton.FillRepositoryItemImageComboBox(repositoryItemImageComboBox1, Singleton.LoaiHang);
            gridControl1.DataSource = Kho_HangHoa.LoadAllBy("CompanyID={0}", Config.CompanyID);
        }

        private void menuButton1_AddNewClick()
        {
            try
            {
                frmHangHoa frm = new frmHangHoa();
                frm.HangHoa = new Kho_HangHoa();
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    IList<Kho_HangHoa> hanghoas = (IList<Kho_HangHoa>)gridControl1.DataSource;
                    hanghoas.Add(frm.HangHoa);
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void menuButton1_DeleteClick()
        {
            Kho_HangHoa hanghoa = (Kho_HangHoa)gridView1.GetFocusedRow();
            if (hanghoa == null)
            {

                Box.Show("Không có dòng nào được chọn");
                return;
            }
            if (Box.Show("Bạn có muốn xóa dòng này không?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                return;
            try
            {
                hanghoa.Delete();
                IList<Kho_HangHoa> hanghoas = (IList<Kho_HangHoa>)gridControl1.DataSource;
                hanghoas.Remove(hanghoa);
                Helpers.Project.SetStatus("Xóa thành công", StatusInfo.Complete);
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Xóa không thành công", StatusInfo.Error);

            }
        }

        private void menuButton1_EditClick()
        {
            Kho_HangHoa hanghoa = (Kho_HangHoa)gridView1.GetFocusedRow();
            if (hanghoa == null)
            {
                menuButton1.Action = GQ.Controls.ActionState.View;
                Box.Show("Không có dòng nào được chọn");
                return;
            }
            try
            {
                frmHangHoa frm = new frmHangHoa();
                frm.HangHoa = hanghoa;
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            menuButton1_EditClick();
        }
    }
}
